<?php
/**
 * Find in files for Windows
 * @filename: searchWin.php
  * @author: skyzhao2009@gmail.com
 * @license:http://www.opensource.org/licenses/bsd-license.php   New BSD License
 * @version: 0.0.4
 */
define(MAX_FILESIZE_M,1024);
define(IMAGE_FRAME_SIZE,16);
ini_set("memory_limit",MAX_FILESIZE_M."M");
if (!class_exists('gtk')) {
    die("Please load the php-gtk2 module in your php.ini\r\n");
}
//Set Window
$wnd = new GtkWindow();
$wnd->set_title('Find in Files(per file < 1G)');
$wnd->set_position(Gtk::WIN_POS_CENTER);
$wnd->set_default_size(600, 600);
$wnd->set_border_width(8);
$pixbuf = $wnd->render_icon(
    Gtk::STOCK_FIND,
    Gtk::ICON_SIZE_DIALOG
);
$wnd->set_icon($pixbuf);
$wnd->connect_simple('destroy', array('gtk', 'main_quit'));

//Option Controls
$txtFilename = new GtkEntry();
$txtKeyword = new GtkEntry();
$txtFilepath = new GtkEntry();

$lblFilename = new GtkLabel('File name match', true);
$lblKeyword = new GtkLabel('Keyword', true);
$lblFilepath = new GtkLabel('File path', true);

$chkFilename = new GtkCheckButton("include path & file name",true);
$chkFilecontent = new GtkCheckButton("check file content",true);
$chkFilename->set_active(true);
$chkFilecontent->set_active(true);

//List Store
$mFile = new GtkListStore(GdkPixbuf::gtype, GObject::TYPE_STRING);
$treeview = new GtkTreeView($mFile);
$columnImg = new GtkTreeViewColumn('Type',new GtkCellRendererPixbuf(),'pixbuf',0);
$column = new GtkTreeViewColumn('File & Path', new GtkCellRendererText(), 'text', 1);
$treeview->append_column($columnImg);
$treeview->append_column($column);

$scrwndFiles = new GtkScrolledWindow();
$scrwndFiles->add($treeview);
$scrwndFiles->set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);

//Button Search
$btnSearch    = GtkButton::new_from_stock('Find in files');
$bbox = new GtkHButtonBox();
$bbox->set_layout(Gtk::BUTTONBOX_SPREAD);
$bbox->add($btnSearch);

//Above Control in Table
$tbl = new GtkTable(6, 4); //5 row 4 col
$xpadding = 30;
$ypadding = 5;
$tbl->attach($lblFilename, 0, 2, 0, 1,Gtk::SHRINK, Gtk::SHRINK, $xpadding, $ypadding); //col 0-2 row 0-1
$tbl->attach($txtFilename, 2, 4, 0, 1,Gtk::SHRINK, Gtk::SHRINK, $xpadding, $ypadding);
$tbl->attach($lblKeyword, 0, 2, 1, 2,Gtk::SHRINK, Gtk::SHRINK, $xpadding, $ypadding);
$tbl->attach($txtKeyword, 2, 4, 1, 2,Gtk::SHRINK, Gtk::SHRINK, $xpadding, $ypadding);
$tbl->attach($lblFilepath, 0, 2, 2, 3,Gtk::SHRINK, Gtk::SHRINK, $xpadding, $ypadding);
$tbl->attach($txtFilepath, 2, 4, 2, 3,Gtk::SHRINK, Gtk::SHRINK, $xpadding, $ypadding);
$tbl->attach($chkFilename, 0, 2, 3, 4,Gtk::SHRINK, Gtk::SHRINK, $xpadding, $ypadding);
$tbl->attach($chkFilecontent, 2, 4, 3, 4,Gtk::SHRINK, Gtk::SHRINK, $xpadding, $ypadding);
$tbl->attach($bbox, 0, 4, 4, 5);

//Event Define
$txtFilename->connect('key-press-event', 'onKeyEnter',$wnd, $mFile, $txtFilename, $txtKeyword, $txtFilepath, $chkFilename, $chkFilecontent);
$txtKeyword->connect('key-press-event', 'onKeyEnter',$wnd, $mFile, $txtFilename, $txtKeyword, $txtFilepath, $chkFilename, $chkFilecontent);
$txtFilepath->connect('key-press-event', 'onKeyEnter',$wnd, $mFile, $txtFilename, $txtKeyword, $txtFilepath, $chkFilename, $chkFilecontent);
$btnSearch->connect_simple('clicked', 'search', $wnd, $mFile, $txtFilename, $txtKeyword, $txtFilepath, $chkFilename, $chkFilecontent);


//Draw Win Control
$frameopt = new GtkFrame('Find options');
$frameopt->add($tbl);
$framerec = new GtkFrame();
$framerec->add($scrwndFiles);

$vbox = new GtkVBox();
$vbox->pack_start($frameopt,false);
$vbox->pack_start($framerec);
$wnd->add($vbox);
$wnd->show_all();
Gtk::main();


function search($wnd, $mFile, $txtFilename, $txtKeyword, $txtFilepath, $chkFilename, $chkFilecontent){

    $mFile->clear();
    $strKeyword = $txtKeyword->get_text();
    $strFilepath = $txtFilepath->get_text();
	$strFilename = $txtFilename->get_text();
	
    $errors = null;
    if (empty($strFilepath)) {
        $errors .= "Filepath is missing.\r\n";
    }	
	
    if (strlen($strKeyword) == 0 && $chkFilecontent->get_active()) {
        $errors .= "Keyword is missing.\r\n";
    }

	
    if ($errors !== null) {
        $dialog = new GtkMessageDialog($wnd, Gtk::DIALOG_MODAL,
            Gtk::MESSAGE_INFO, Gtk::BUTTONS_OK, $errors);
        $dialog->set_markup(
            "The following errors occured:\r\n"
            . "<span foreground='red'>" . $errors . "</span>"
        );
        $dialog->run();
        $dialog->destroy();
    }
	else {
	     if (empty($strFilename)) $strFilename = "*.*";
         if (substr($strFilepath,-1)!='/') $strFilepath .= '/';
	     $strFilename = sql_regcase($strFilename);
	
         $arrResults = Array();
         if ($chkFilename->get_active()) 
            $arrResults = searchPathNameByKeyword($strFilename,$strFilepath,$strKeyword,$mFile);
	
         if ($chkFilecontent->get_active()) 
            searchFileContentByKeyword($strFilename,$strFilepath,$strKeyword,$mFile,$arrResults);	
    }
	
}
	

function searchPathNameByKeyword($strFilename,$path,$keyword,$mFile){
$arrResults = Array();
if (is_dir($path)){
    if (substr($path,-1)!='/') $path .= '/';
   foreach (glob($path.$strFilename) as $filename) { 
			if (stripos($filename,$keyword)>0 || empty($keyword)){
                $pb = GdkPixbuf::new_from_file("images/txt.gif")->scale_simple(IMAGE_FRAME_SIZE, IMAGE_FRAME_SIZE, Gdk::INTERP_BILINEAR);			
				$mFile->append(array($pb,$filename));
				$arrResults[] = $filename;
			}        
   }
   $fp=opendir($path);  
    while (($file = readdir($fp)) !== false) {
        if ($file=="." || $file=="..") continue;
		if (substr($path,-1)!='/') $path .= '/';
		    if (is_dir($path.$file))
			{
			    if (stripos($path.$file,$keyword)>0 || empty($keyword)){
				    $pb = GdkPixbuf::new_from_file("images/folder.gif")->scale_simple(IMAGE_FRAME_SIZE, IMAGE_FRAME_SIZE, Gdk::INTERP_BILINEAR);
                    $mFile->append(array($pb,$path.$file));

			    }        			
			    searchPathNameByKeyword($strFilename,$path.$file,$keyword,$mFile);
			}
        }
        closedir($fp);
}
return $arrResults;
}

function searchFileContentByKeyword($strFilename,$path,$keyword,$mFile,$arrResults){

if (is_dir($path)){
   if (substr($path,-1)!='/') $path .= '/';
   foreach (glob($path.$strFilename) as $filename) {
                if (filesize($filename)>MAX_FILESIZE_M) continue;
			    $fileContent = file_get_contents($filename);
			    if (stripos($fileContent,$keyword)>0 || empty($keyword)){
				   if (!in_array($filename, $arrResults)) {
				   $pb = GdkPixbuf::new_from_file("images/txt.gif")->scale_simple(IMAGE_FRAME_SIZE, IMAGE_FRAME_SIZE, Gdk::INTERP_BILINEAR);			
				   $mFile->append(array($pb,$filename));
				   }
			    }
   }
   
   $fp=opendir($path);  
        while (($file = readdir($fp)) !== false) {
		    if ($file=="." || $file=="..") continue;
			if (substr($path,-1)!='/') $path .= '/';
			   searchFileContentByKeyword($strFilename,$path.$file,$keyword,$mFile,$arrResults);
        }
        closedir($fp);
}
}

function onKeyEnter($widget, $event, $wnd, $mFile, $txtFilename, $txtKeyword, $txtFilepath, $chkFilename, $chkFilecontent){
if ($event->keyval==65293){
   search($wnd, $mFile, $txtFilename, $txtKeyword, $txtFilepath, $chkFilename, $chkFilecontent);
}


}
?>
